import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class InsertWin extends JFrame implements ActionListener{
    String user;
    Box label_box,field_box;
    JLabel label_Number,label_Name,label_Grade,label_Hometown;
    JTextField field_Number,field_Name,field_Grade,field_Hometown;
    JButton  insert,exit;
    Connection con;
    Statement sql;
    ResultSet rs;
    OperationWin operationWin;
    InsertWin(String title,String user){
        setTitle(title);
        this.user=user;

        insert=new JButton("插入记录");
        exit=new JButton("退出");
        insert.addActionListener(this);
        exit.addActionListener(this);

        label_Number=new JLabel("请输入学号:");
        label_Name=new JLabel("请输入姓名:");
        label_Grade=new JLabel("请输入成绩:");
        label_Hometown=new JLabel("请输入家乡:");

        field_Number=new JTextField(50);
        field_Name=new JTextField(50);
        field_Grade=new JTextField(50);
        field_Hometown=new JTextField(50);

        label_box=Box.createVerticalBox();
        field_box=Box.createVerticalBox();

        label_box.add(label_Number);
        label_box.add(Box.createVerticalStrut(30));
        label_box.add(label_Name);
        label_box.add(Box.createVerticalStrut(30));
        label_box.add(label_Grade);
        label_box.add(Box.createVerticalStrut(30));
        label_box.add(label_Hometown);

        field_box.add(field_Number);
        field_box.add(Box.createVerticalStrut(20));
        field_box.add(field_Name);
        field_box.add(Box.createVerticalStrut(20));
        field_box.add(field_Grade);
        field_box.add(Box.createVerticalStrut(20));
        field_box.add(field_Hometown);

        setLayout(null);
        add(label_box);
        add(field_box);
        add(insert);
        add(exit);

        label_box.setBounds(110,50,100,280);
        field_box.setBounds(230,45,150,165);
        insert.setBounds(120,250,100,30);
        exit.setBounds(260,250,100,30);

        setBounds(700,400,500,350);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent event){
        if(event.getSource()==exit){
            if (JOptionPane.showConfirmDialog(this, "是否退出", "提示", JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION) {
                dispose();
                operationWin=new OperationWin("系统操作界面",user);
            }
        }
        else if(event.getSource()==insert){
            String Number,Name,Grade,Hometown;
            Number=field_Number.getText().trim();
            Name=field_Name.getText().trim();
            Grade=field_Grade.getText().trim();
            Hometown=field_Hometown.getText().trim();

            try{
                con= DriverManager.getConnection("jdbc:sqlserver://localhost:49689;DatabaseName=Student","sa","061210");
                sql=con.createStatement();
                rs=sql.executeQuery("SELECT * FROM student WHERE Number = '"+Number+"'");
                if(rs.next()){
                    JOptionPane.showMessageDialog(this,"该学号已存在","WARNING",JOptionPane.WARNING_MESSAGE);
                }
                else {
                    sql.executeUpdate("INSERT INTO student(Number,Name,Grade,Hometown) VALUES('" + Number + "','" + Name + "','" + Grade + "','" + Hometown + "')");
                    JOptionPane.showMessageDialog(this, "\t插入成功", "SUCCESS", JOptionPane.PLAIN_MESSAGE);
                    label_Number.setText("");
                    label_Name.setText("");
                    label_Grade.setText("");
                    label_Hometown.setText("");
                }
            }
            catch(SQLException e){
                JOptionPane.showMessageDialog(this,e,"WARNING",JOptionPane.WARNING_MESSAGE);
                System.exit(0);
            }
        }
    }
}
